---
slug: expando-object
title: 4. 粘土对象
author: dotNET China
author_title: 为中国 .NET 开发者提供优质的资讯和技术分享。
author_url: https://www.chinadot.net
author_image_url: https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png
tags: [furion, furos, .net, .netcore, .net5, clay, ExpandoObject, dictionary]
---

import useBaseUrl from "@docusaurus/useBaseUrl";

## 对象转 `IDictionary<string, object>`

有些时候我们需要将一个对象或匿名类转换成字典类型，就需要用到该功能。

```cs {4}
var dic = new {
    Id = 1,
    Name = "Furion"
}.ToDictionary();

foreach (var key in dic)
{
    Console.WriteLine(dic[key]);
}
```

## 对象转 `IDictionary<string, Tuple<Type, object>>`

```cs {4}
var dic = new {
    Id = 1,
    Name = "Furion"
}.ToDictionaryWithType();

foreach (var key in dic)
{
    Console.WriteLine(dic[key].Item1);
    Console.WriteLine(dic[key].Item2);
}
```

## 对象转 `ExpandoObject` 类型

```cs
dynamic expando = new {
    Id = 1,
    Name = "百小僧",
    Project = new {
        Name = "Furion"
    }
}.ToExpandoObject();

// 动态添加属性
expando.NickName = "MonkSoul";
expando.Project.Id = 1;

Console.WriteLine(expando.Name);    // => 百小僧
Console.WriteLine(expando.Project.NickName);    // => MonkSoul
```
